package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;


public class CodecDate extends CodecDefault{


    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue{
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        var d:Date = getMemberFromObject(obj, descProperty) as Date;
        
        if( d != null ){
	        rt.value = d;
	        rt.string = d.toDateString();
	        rt.code = "new Date( " + d.getFullYear()
	                    + ", " + d.getMonth()
	                    + ", " + d.getDate()
	                    + ", " + d.getHours()
	                    + ", " + d.getMinutes()
	                    + ", " + d.getSeconds()
	                    + ", " + d.getMilliseconds() + ")";
        }
        else{
        	rt.value = null;
        	rt.string = null;
        	rt.code = "null"
        }
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        if (inString) {
        	if( value == null ){
        		obj[ descProperty.name ] = value;
        	}
        	else{
            	obj[ descProperty.name ] = new Date( Date.parse( String( value )));
         	}
        }
        else {
            obj[ descProperty.name ] = value;
        }
    }
}
}